# /usr/bin/env python3.6
# -*- encoding: utf-8 -*-
"""
@File    : cinema_user_model.py
@Time    : 2020/2/7 0007 13:35
@Author  : zhangbing
@Email   : 18829172841@163.com
@Software: PyCharm
"""
from werkzeug.security import generate_password_hash, check_password_hash

from App.ext import db
from App.models import BaseModel


class CinemaUser(BaseModel):
    username = db.Column(db.String(32), unique=True)
    _password = db.Column(db.String(256))
    phone = db.Column(db.String(32), unique=True)
    is_delete = db.Column(db.Boolean, default=False)
    is_verify = db.Column(db.Boolean, default=False)  # 是否能够进行对影院的操作

    @property
    def password(self):
        raise Exception("can't  access")

    @password.setter
    def password(self, password_value):
        self._password = generate_password_hash(password_value)

    def check_password(self, password_value):
        return check_password_hash(self._password, password_value)

    def check_permssion(self, permission):
        if not self.is_verify:
            return False
        permissions = CinemaUserPermission.query.filter_by(c_user_id=self.id).all()  # 此处获取到的是该用户对应的权限
        # 的列表，通过其中c_permission_id  可以获取其中的对应的权限,进而进行相关的查询操作
        for user_permission in permissions:
            if permission == Perssions.query.get(user_permission.c_permission_id).p_name:  # 此处获取到其对应的name
                return True
        return False


class Perssions(BaseModel):
    p_name = db.Column(db.String(64), unique=True)


class CinemaUserPermission(BaseModel):
    c_user_id = db.Column(db.Integer, db.ForeignKey(CinemaUser.id))
    c_permission_id = db.Column(db.Integer, db.ForeignKey(Perssions.id))


if __name__ == "__main__":
    pass
